//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core/index.js')
const lib = Pager.lib
const util = require('../../utils/util.js')

let mkScrenn = require('components/modules/screen')
let mkDropdownList = require('components/modules/dropdownlist')
const xx0 = mkDropdownList({$$id: 'dd-list-radio-0', data: [{title: '从远到近', idx: 'a1'}, {title: '从近到远', idx: 'b1'}, {title: '附近', idx: 'c1'}], parentId: '0', type: 'radio'})
const xx1 = mkDropdownList({$$id: 'dd-list-checkbox', data: [{title: '200m以内的', idx: 'a1'}, {title: '500m以内的', idx: 'b1'}, {title: '1km以内的', idx: 'c1'}], parentId: '1', type: 'checkbox'})
const xx2 = mkDropdownList({$$id: 'dd-list-radio-2', data: [{title: '深圳', idx: 'a1'}, {title: '广州', idx: 'b1'}, {title: '上海', idx: 'c1'}], parentId: '0', type: 'radio'})
const xx3 = mkDropdownList({$$id: 'dd-list-radio-3', data: [{title: '便宜', idx: 'a1'}, {title: '舒适', idx: 'b1'}, {title: '品质', idx: 'c1'}], parentId: '0', type: 'radio'})

//模拟数据
import {data1} from './data'

Pager({
  data: {
    clicked: false,
    head: Pager.list({$$id: 'head', listClass: 'list-tabs-1', itemClass: 'item-title', listStyle: `height: ${app.globalData.navheight}px; padding-top: ${app.globalData.statusBarHeight}px`, header: {
      title: ' ', itemClass: 'icon-search boxer-serach', aim: 'onJump?type=search'
    }}),
    tab2: Pager.list({$$id: 'tab2', listClass: 'list-tabs', itemClass: 'item-title'}),
    body: Pager.list({$$id: 'body', listClass: 'bg-fff height-bar', itemClass: 'lr-item mlr-default', type: { is: 'scroll', 'scroll-y': true}}),
    pages: mkScrenn({
      $$id: 'dd-screen',
      menus: [{title: '位置', type: 'radio'}, {title: '车程距离', type: 'checkbox'}, {title: '热门城市', type: 'c'}, {title: '自驾主题', type: 'd'}],
      contents: [
        xx0,xx1, xx2, xx3
      ],
      itemClass: 'dropdown-item-content',
      itemWidth: '25%'
      // mask: false
    })
  },
  onJump(e, params, inst) {
    const _type = params.type || 'detail'
    wx.navigateTo({
      url: '../'+_type+'/index?from=list'
    })
  },
  onMenus(e, params, inst) {
    const that = this
    const type = params.id.substr(0, params.id.length - 1)+'1'
    // const typeTab = params.id.substr(0, params.id.length - 1)
    let _bData = adapterListLR(data1.bodyList.find(item => item.type === type).data)
    let data = type.indexOf('gd') > -1 ? this.tab2Data : adapterMenus([{title: "文艺演出1", type: 'gat1'}, {title: "文艺演出2", type: 'gat2'}])
    this.tab2.update(data)
    this.body.update([{title: [{title: '', itemClass: 'animate-load-row'}], titleClass: 'wid-p100 hei-tabHei flex-row-center-center'}], () => {
      setTimeout(() => {
        that.body.update({data: _bData, listClass: 'bg-fff height-bar', itemClass: 'lr-item mlr-default'})
      }, 500);
    })
    
    inst.siblings().removeClass('active')
    inst.addClass('active')
  },
  onReady: function() {
    util.insertScreenAD('adunit-1aaa4a38a649de35')
    this.head = this.getElementsById('head')
    this.tab2 = this.getElementsById('tab2')
    this.body = this.getElementsById('body')
    this.head.update(this.headData)
    this.tab2.update(this.tab2Data)
    this.body.update(this.bodyData)
    //筛选
    const that = this
    const $radioInst0 = this.getElementsById('dd-list-radio-0')
    const $checkInst = this.getElementsById('dd-list-checkbox')
    this.$screenInst = this.getElementsById('dd-screen')

    $checkInst.hooks.on('keep-val', function(params) {
      const _vals = params.vals
      const _parentId = params.parentId
      that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    })

    $radioInst0.hooks.on('dropdownlist-radio', function(params) {
      const _vals = params.vals
      const _parentId = params.parentId
      that.$screenInst.onClose({parentId: _parentId, vals: _vals})
    })
  },
  onLoad: function() {
    this.headData = adapterMenus(data1.headList)
    this.tab2Data = adapterMenus([{title: "明星", type: 'gd1'}, {title: "明星演出2", type: 'gd2'}, {title: "明星演出3", type: 'gd3'}, {title: "明星演出4", type: 'gd4'}])
    this.bodyData = adapterListLR(data1.bodyList.find(item => item.type === 'gd1').data)
  },
  $screenInst: null,
  onAppend(e, params, inst) {
    //重置按钮
    console.log(this.$screenInst, xx3)
  },
  onItem(e, params, inst) {
    const type = params.type
    const idx = params.idx
    inst.addClass('active')
    if (type === 'radio') {
      inst.siblings().removeClass('active')
    }
  },
  
})

const adapterMenus = (data, isAll) => {
  let output = []
  if (isAll) {
    output.unshift({
      title: '全部',
      aim: 'onMenus?id=0',
      itemClass: 'active'
    })
  }
  data.map((item, ii) => {
    output.push({
      title: item.title,
      aim: 'onMenus?id='+item.type,
      itemClass: !isAll && ii === 0 ? 'active' : ''
    })
  })
  return output
}

const adapterListLR = (data) => {
  //左边图，右边内容
  return data.map(item => {
    return {
      img: { src: item.img, itemClass: 'item-pic', mode: 'aspectFill' },
      body: [
        {title: item.title+ '李大奔“FEVER · 烧”Special LIVE', itemClass: 'title-default textellipsis-2'},
        {
          title: [
            { title: '2020.01.01-2020.05.05', itemClass: 'item-time'},
            { title: '上海', itemClass: 'color-info'},
          ],
          titleClass: 'flex-row-between-center'
        }
      ],
      aim: 'onJump?id='+item.id,
      bodyClass: 'flex-1 flex-column-between'
    }
  })
}


